<script>
import { User, EditPen } from '@element-plus/icons-vue'
import userRules from '@/rules/registerRules.js'
// import axios from 'axios'
import { useAuthStore } from '@/stores/authStore.js'
import { mapWritableState } from 'pinia'
import { login } from '@/api/authApi'
export default {
  components: {
    User,
    EditPen,
  },

  data() {
    return {
      user: {
        username: '',
        password: '',
        usertype: '1',
      },
      userRules: userRules,
    }
  },
  computed: {
    ...mapWritableState(useAuthStore, ['userA', 'token']),
  },

  methods: {
    async doLogin() {
      // 1. 校验数据
      await this.$refs.loginForm.validate()
      // 2. 发送登录请求
      // let result = await login(this.user)
      let data = await login(this.user)
      // console.log(result.data.data.user)
      // console.log(result.data.data.token)
      // this.userA = result.data.data.user
      console.log(data)

      this.userA = data.user
      // this.token = result.data.data.token
      this.token = data.token
      this.$router.push({
        name: 'home',
      })
    },
  },
}
</script>
<template>
  <div class="box">
    <el-card>
      <template #header> 课程管理系统 </template>
      <el-form :model="user" :rules="userRules" ref="loginForm">
        <el-form-item class="input-item" prop="username">
          <el-input placeholder="用户名" size="large" v-model="user.username">
            <template #prefix>
              <el-icon>
                <el-icon><User /></el-icon>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="密码"
            type="password"
            size="large"
            v-model="user.password"
            show-password
          >
            <template #prefix>
              <el-icon><EditPen /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="user.usertype">
            <el-radio-button label="student" value="3">学生</el-radio-button>
            <el-radio-button label="teacher" value="2">教师</el-radio-button>
            <el-radio-button label="admin" value="1">管理员</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item class="register">
          <el-button type="primary" @click="doLogin()">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<style scoped>
.box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('@/assets/img/login-background.jpg');
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}
.el-card {
  width: 350px;
  padding: 5px 0;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
}

.el-form-item {
  margin: 20px 0;
}
.input-item {
  margin-top: 0;
}
.register {
  text-align: center;
  margin-block: 0;
}
.el-button {
  width: 100%;
}
.el-radio-group {
  display: flex;
  justify-content: space-between;
  width: 100%;
}
.el-radio-button {
  flex-grow: 1;
}
.el-radio-button :deep(.el-radio-button__inner) {
  width: 100%;
  text-align: center;
}
</style>
